package com.asap.demo.model;

import com.alibaba.fastjson.JSONException;
import com.asap.demo.interf.Event;
import com.asap.demo.utils.JsonParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * @author wangwei
 *
 */
public class StandardEvent implements Event,Serializable {
	private static Logger logger = LoggerFactory.getLogger(StandardEvent.class);
	/**
	 * @Fields: serialVersionUID
	 * @Todo: TODO
	 */
	private static final long serialVersionUID = -8073354183428410665L;
	private Map<String, Object> map = new LinkedHashMap<>();
	private List<String> fittedRules = new ArrayList<>();


	public StandardEvent() {
		//this.id = UUID.randomUUID().toString();
		//this.map.put("uuid", this.id);
	}

	public StandardEvent(Map<String, Object> map) {

		this.map.putAll(map);
	}

	/**
	 *
	 * @param json json
	 * @return StandardEvent
	 */
	public static StandardEvent parse(String json) {
		StandardEvent event;
		try{
			event = new StandardEvent(
					JsonParser.<String,Object>parseFrom(json));
		}catch(JSONException e){
			logger.error("parse json error:"+json);
			e.printStackTrace();
			event=new StandardEvent();
		}
		return event;
	}


	@Override
	public String getField(String fieldName) {
		Object o=this.map.get(fieldName);
		if(o == null)
			return null;
		else
			return o.toString();
	}

	@Override
	public void addField(String fieldName, Object fieldValue) {

		this.map.put(fieldName, fieldValue);
	}
	@Override
	public void addMap(Map<String, Object> map) {

		this.map.putAll(map);
	}
	@Override
	public String toString() {

		return JsonParser.<String,Object>toJson(this.map);
	}


	/**
	 *
	 * @param fieldName field
	 * @param fieldValue value
	 */
	public void updateField(String fieldName, String fieldValue) {

		if (!this.map.containsKey(fieldName)) {
			return;
		}
		this.addField(fieldName, fieldValue);
	}

	public List<String> getFittedRules() {
		return fittedRules;
	}

	void setFittedRules(List<String> fittedRules) {
		this.fittedRules = fittedRules;
	}

	@Override
	public Map<String, Object> getAllFields() {

		return this.map;
	}
	@Override
	public void delField(String field) {
		this.map.remove(field);

	}

}